package org.jeecgframework.web.system.pojo.base;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.ExcelUtil;
import org.jeecgframework.web.util.StaticValue;

/**
 * @author xiul
 * 2016-6-6 下午5:08:15
 * Description 
 */
public class ZyUpEquipmentThread extends Thread{
	
	public final static String servering = "服务中";
	public final static String notUsed = "未使用";
	public final static String stop = "暂停服务";
	
	private static SystemService systemService = ApplicationContextUtil.getBean("systemService");
	
	private int beginR;
	private int endR;
	private XSSFSheet sheet;
	
	public ZyUpEquipmentThread(){}

	/**
	 * @param beginRow 开始行数
	 * @param endRow 结束行数
	 * @param sheet sheet页
	 */
	public ZyUpEquipmentThread(int beginRow, int endRow, XSSFSheet sheet){
		this.beginR = beginRow;
		this.endR = endRow;
		this.sheet = sheet;
	}
	
	public void run(){
		long begin = System.currentTimeMillis();
		System.out.println("\t,开始解析："+ beginR +"\t至\t"+ endR);
		List<ZyEquipmentTypeEntity> esTypes = systemService.getList(ZyEquipmentTypeEntity.class);
		List<ZyCinemaEntity> cms = systemService.getList(ZyCinemaEntity.class);
		List<ZyRoomEntity> rms = systemService.getList(ZyRoomEntity.class);
		List<TSDepart> tsDeparts = systemService.getList(TSDepart.class);
		Map<String, TSDepart> dpMap = new HashMap<String, TSDepart>();
		for(TSDepart dp : tsDeparts){
			dpMap.put(dp.getId(), dp);
		}
		SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
		List<ZyEquipmentSpareEntity> result = new ArrayList<ZyEquipmentSpareEntity>();
		try {
			for(int r = beginR; r <= endR; r++) {
				ZyCinemaEntity zyCinema = null;
				XSSFRow row = sheet.getRow(r);
				if(null != row){
					String serverState = "";
					ZyEquipmentSpareEntity zyEquipmentSpareEntity = new ZyEquipmentSpareEntity();
					zyEquipmentSpareEntity.setSn(ExcelUtil.getCellValue(row.getCell(0)));
					zyEquipmentSpareEntity.setItem(ExcelUtil.getCellValue(row.getCell(1)));
					zyEquipmentSpareEntity.setTopItemSn(ExcelUtil.getCellValue(row.getCell(2)));
					String cinemaName = ExcelUtil.getCellValue(row.getCell(4));
					zyCinema = null;
					for (ZyCinemaEntity cm : cms) {
						if(cm.getName().equals(cinemaName)){
							zyCinema = cm;
							break;
						}
					}
					if(StringUtil.isNotEmpty(zyCinema)){
						zyEquipmentSpareEntity.setZyCinema(zyCinema);
						zyEquipmentSpareEntity.setTsTerritory(zyCinema.getProvince());
					}else{
						zyEquipmentSpareEntity.setZyCinema(null);
						zyEquipmentSpareEntity.setTsTerritory(null);
					}
					
					ZyRoomEntity zyRoom = null;
					for (ZyRoomEntity rm : rms) {
						if(null != rm.getRoomCinema() && 
								rm.getRoomCinema().getName().equals(cinemaName) &&
									rm.getRoomNum().equals(ExcelUtil.getCellValue(row.getCell(5)))){
							zyRoom = rm;
							break;
						}
					}
					
					zyEquipmentSpareEntity.setHouse(zyRoom);
					zyEquipmentSpareEntity.setInstallDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(6)), ft));
					zyEquipmentSpareEntity.setBaseRepairDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(7)), ft));
					zyEquipmentSpareEntity.setSendDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(8)), ft));
					zyEquipmentSpareEntity.setRepairEndDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(9)), ft));
										
					serverState = ExcelUtil.getCellValue(row.getCell(10));
					switch(ExcelUtil.getCellValue(row.getCell(10))){
					case servering : 
						serverState = StaticValue.DIC_SERVER_STATE_1001;
						break;
					case notUsed : 
						serverState = StaticValue.DIC_SERVER_STATE_1002;
						break;
					case stop : 
						serverState = StaticValue.DIC_SERVER_STATE_1003;
						break;
					default:
						serverState = "";
					}
					zyEquipmentSpareEntity.setServerState(serverState);
					zyEquipmentSpareEntity.setRemarks(ExcelUtil.getCellValue(row.getCell(11)));
					zyEquipmentSpareEntity.setIsNormal(ExcelUtil.getCellValue(row.getCell(12)).equals("TRUE")?"1":"0");
					zyEquipmentSpareEntity.setExceptionReason(ExcelUtil.getCellValue(row.getCell(13)));
					zyEquipmentSpareEntity.setBpNo(ExcelUtil.getCellValue(row.getCell(14)));
					for(TSDepart depart: tsDeparts){
						if(StringUtil.isNotEmpty(zyEquipmentSpareEntity.getBpNo()) && 
								depart.getBpCode().equals(zyEquipmentSpareEntity.getBpNo())){
							zyEquipmentSpareEntity.setTsDepart(depart);//根据BPno反向设置 所属机构字段
							break;
						}
					}
					
					String esType = ExcelUtil.getCellValue(row.getCell(15));
					ZyEquipmentTypeEntity zyEquipmentTypeEntity = null;
					for (ZyEquipmentTypeEntity es : esTypes) {
						if(es.getTypeDesc().equals(StringUtil.getString(esType))){
							zyEquipmentTypeEntity = es;
							break;
						}
					}
					zyEquipmentSpareEntity.setType(zyEquipmentTypeEntity);
					zyEquipmentSpareEntity.setIsServicing(ExcelUtil.getCellValue(row.getCell(16)).equals("TRUE")?"1":"0");
					if((null!=zyEquipmentSpareEntity.getTopItemSn() && !zyEquipmentSpareEntity.getTopItemSn().equals("")) && !zyEquipmentSpareEntity.getSn().equals(zyEquipmentSpareEntity.getTopItemSn())){
						zyEquipmentSpareEntity.setIsSpare(true);
					}else{
						zyEquipmentSpareEntity.setIsSpare(false);
						zyEquipmentSpareEntity.setLaserReconstruction("0");
					}
					zyEquipmentSpareEntity.setWarrantyBeginDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(17)), ft));
					zyEquipmentSpareEntity.setCompanyOfRenewal(ExcelUtil.getCellValue(row.getCell(18)));
					zyEquipmentSpareEntity.setRepairDate(StringUtil.isInteger(ExcelUtil.getCellValue(row.getCell(19))) ? Integer.valueOf(ExcelUtil.getCellValue(row.getCell(19))) : 0);
					zyEquipmentSpareEntity.setSendSign(dpMap.get(ExcelUtil.getCellValue(row.getCell(20))));
					zyEquipmentSpareEntity.setIsHadInspection("0");//默认没有巡检计划
					result.add(zyEquipmentSpareEntity); 
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		systemService.batchSave(result);
		long end = System.currentTimeMillis();
		System.out.println("设备Excel 解析完毕，用时："+(end - begin) / 1000 +"." + (end - begin) % 1000 +"秒");
	}

	public static SystemService getSystemService() {
		return systemService;
	}
	public static void setSystemService(SystemService systemService) {
		ZyUpEquipmentThread.systemService = systemService;
	}

	public int getBeginR() {
		return beginR;
	}
	public void setBeginR(int beginR) {
		this.beginR = beginR;
	}

	public int getEndR() {
		return endR;
	}
	public void setEndR(int endR) {
		this.endR = endR;
	}

	public XSSFSheet getSheet() {
		return sheet;
	}
	public void setSheet(XSSFSheet sheet) {
		this.sheet = sheet;
	}
	
}
